//柱状图组件
import * as echarts from 'echarts';
import { useEffect, useRef } from 'react';
//1.把功能代码都放到这个组件中
//2.把可变的部分抽象成prop参数
const BarChart = ({ title, dataName, dataNum }) => {
    const chartRef = useRef(null)
    useEffect(() => {
        //1.获取渲染图标的dom节点
        const chartDom = chartRef.current
        //2.图标初始化生成图标实例对象
        const myChart = echarts.init(chartDom);

        //图标参数
        const option = {
            title: {
                text: title
            },
            xAxis: {
                type: 'category',
                data: dataName
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    data: dataNum,
                    type: 'bar'
                }
            ]
        };
        //使用图表的参数完成图表的渲染
        option && myChart.setOption(option);




    }, [title, dataName, dataNum])
    return <div ref={chartRef} style={{ width: '500px', height: '400px' }}></div>
}


export default BarChart